package com.obao.user.action;

import com.obao.business.entity.Product;
import com.obao.user.entity.Collect;
import com.obao.user.entity.User;
import com.obao.user.service.ICollectService;
import com.opensymphony.xwork2.ActionSupport;

import java.util.Date;
import java.util.List;

/**
 * author 代江平
 * create Date 2016/11/28
 */
public class CollectAction extends ActionSupport {
    private User user = null;
    private Product product = null;
    private Collect collect = null;
    private ICollectService collectService;
    private List<Collect> collects = null;


    /**
     * 添加收藏/取消收
     *
     * @return
     */
    public String add() {
        try {
            if (user != null && product != null) {
                Integer id = collectService.isExist(product,user);
                System.out.println(id);
                if(id != 0){
                    collectService.delete(id);
                }else{
                    Collect collect = new Collect();
                    collect.setUser(user);
                    collect.setProduct(product);
                    collect.setAddDate(new Date());
                    collectService.save(collect);
                }
                return SUCCESS;
            }else{
                return ERROR;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }
    }
    public String findUserAll(){
       try {
           collects = collectService.findUserAllCollect(user);
       }catch (Exception e){
           e.printStackTrace();
       }
        return "myCollect";
    }

    public List<Collect> getCollects() {
        return collects;
    }

    public void setCollects(List<Collect> collects) {
        this.collects = collects;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public Product getProduct() {
        return product;
    }

    public void setProduct(Product product) {
        this.product = product;
    }

    public ICollectService getCollectService() {
        return collectService;
    }

    public void setCollectService(ICollectService collectService) {
        this.collectService = collectService;
    }

    public Collect getCollect() {
        return collect;
    }

    public void setCollect(Collect collect) {
        this.collect = collect;
    }


}

